﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using XWord.Fundamental.Domain;
using NHibernate.Criterion;
using NHibernate;
using NHibernate.Transform;
using XWord.GUI.Views;

namespace WordAssistantApp
{
    /// <summary>
    /// Interaction logic for EtymaView.xaml
    /// </summary>
    public partial class EtymaView : UserControl
    {
        
        public EtymaView()
        {
            InitializeComponent();
            if(System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            Reflash();
        }

        private void Reflash()
        {
            var list = UnitOfWork.CurrentSession.CreateCriteria<Etymon>()
   .Add(Restrictions.IsNull("Parent"))
   .SetFetchMode("Children", FetchMode.Eager)
   .SetResultTransformer(new DistinctRootEntityResultTransformer()).AddOrder(Order.Asc("Name"))
   .List<Etymon>();

            this.DataContext = new ObservableCollection<Etymon>(list);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            Etymon entity = element.DataContext as Etymon;
            EtymonEditView win = new EtymonEditView();
            win.textBox1.Text = entity.Name;
            win.textBox2.Text = entity.Comment;
            win.EditEtyma = entity;
            win.DataContext = entity;
            win.IsEdit = true;
            win.ShowDialog();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            Etymon entity = element.DataContext as Etymon;
            EtymonEditView win = new EtymonEditView();
            BindingOperations.ClearBinding(win.textBox1, TextBox.TextProperty);
            
            win.textBox1.Text = entity.Name;
            win.textBox2.Text = entity.Comment;
            win.EditEtyma = entity;
            
            win.IsEdit = false;
            win.ShowDialog();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            Etymon entity = element.DataContext as Etymon;
            if (entity.Parent!=null)
            {
                entity.Parent.Children.ToList().Remove(entity);
                
            }
            else
            {
                (this.DataContext as ObservableCollection<Etymon>).Remove(entity);

            }
            UnitOfWork.CurrentSession.Clear();
            UnitOfWork.CurrentSession.Delete(entity);
            UnitOfWork.CurrentSession.Flush();
        }
        public event RoutedPropertyChangedEventHandler<object> TreeSelectedItemChangedEvent
        {
            add { tree.AddHandler(TreeView.SelectedItemChangedEvent, value); }
            remove { tree.RemoveHandler(TreeView.SelectedItemChangedEvent, value); }
        }
       
        public event  EventHandler AddWord;

        public Guid[] SelectedWords { get; set; }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {

            AddWord(sender, null);
            FrameworkElement element = sender as FrameworkElement;
            Etymon entity = element.DataContext as Etymon;
            //dc.ExecuteCommand(
            //        string.Format("delete EtymaWord where WordId='{0}'", entity.Id));
            foreach (var item in SelectedWords)
            {
                if (!entity.Words.OfType<Word>().Any(ee => ee.Id == item))
                {

                    UnitOfWork.CurrentSession.CreateSQLQuery(
                        string.Format("insert into EtymaWord (EtymaId,WordId) values ('{0}','{1}')", entity.Id, item)).ExecuteUpdate();
                }
            }
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            EtymonEditView win = new EtymonEditView();
            win.ShowDialog();
            Reflash();

        }
    }
}
